{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 安装第三方库\n",
    "!pip install pygraphviz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from utils import Scalar, draw_graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\n",
       " -->\n",
       "<!-- Title: %3 Pages: 1 -->\n",
       "<svg width=\"178pt\" height=\"164pt\"\n",
       " viewBox=\"0.00 0.00 178.21 163.84\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 159.844)\">\n",
       "<title>%3</title>\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-159.844 174.205,-159.844 174.205,4 -4,4\"/>\n",
       "<!-- 140288582452992forward -->\n",
       "<g id=\"node1\" class=\"node\"><title>140288582452992forward</title>\n",
       "<polygon fill=\"lightgreen\" stroke=\"black\" stroke-width=\"2\" points=\"0,-0.5 0,-59.4219 76.2051,-59.4219 76.2051,-0.5 0,-0.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-47.4219\" font-family=\"Menlo\" font-size=\"10.00\">grad=None</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"0,-39.7812 76.2051,-39.7812 \"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-27.7812\" font-family=\"Menlo\" font-size=\"10.00\">value=2.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"0,-20.1406 76.2051,-20.1406 \"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-8.14062\" font-family=\"Menlo\" font-size=\"10.00\">b</text>\n",
       "</g>\n",
       "<!-- 140288582453136forward -->\n",
       "<g id=\"node2\" class=\"node\"><title>140288582453136forward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M59,-96.4219C59,-96.4219 111.205,-96.4219 111.205,-96.4219 117.205,-96.4219 123.205,-102.422 123.205,-108.422 123.205,-108.422 123.205,-143.344 123.205,-143.344 123.205,-149.344 117.205,-155.344 111.205,-155.344 111.205,-155.344 59,-155.344 59,-155.344 53,-155.344 47,-149.344 47,-143.344 47,-143.344 47,-108.422 47,-108.422 47,-102.422 53,-96.4219 59,-96.4219\"/>\n",
       "<text text-anchor=\"middle\" x=\"85.1025\" y=\"-143.344\" font-family=\"Menlo\" font-size=\"10.00\">grad=None</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"47,-135.703 123.205,-135.703 \"/>\n",
       "<text text-anchor=\"middle\" x=\"85.1025\" y=\"-123.703\" font-family=\"Menlo\" font-size=\"10.00\">value=3.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"47,-116.062 123.205,-116.062 \"/>\n",
       "<text text-anchor=\"middle\" x=\"85.1025\" y=\"-104.062\" font-family=\"Menlo\" font-size=\"10.00\">+</text>\n",
       "</g>\n",
       "<!-- 140288582452992forward&#45;&gt;140288582453136forward -->\n",
       "<g id=\"edge2\" class=\"edge\"><title>140288582452992forward&#45;&gt;140288582453136forward</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M52.4595,-59.6509C56.8328,-68.3903 61.7066,-78.1298 66.3264,-87.3617\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"63.2284,-88.992 70.8335,-96.3685 69.4883,-85.8594 63.2284,-88.992\"/>\n",
       "</g>\n",
       "<!-- 140288582453088forward -->\n",
       "<g id=\"node3\" class=\"node\"><title>140288582453088forward</title>\n",
       "<polygon fill=\"lightgreen\" stroke=\"black\" stroke-width=\"2\" points=\"94,-0.5 94,-59.4219 170.205,-59.4219 170.205,-0.5 94,-0.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"132.103\" y=\"-47.4219\" font-family=\"Menlo\" font-size=\"10.00\">grad=None</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"94,-39.7812 170.205,-39.7812 \"/>\n",
       "<text text-anchor=\"middle\" x=\"132.103\" y=\"-27.7812\" font-family=\"Menlo\" font-size=\"10.00\">value=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"94,-20.1406 170.205,-20.1406 \"/>\n",
       "<text text-anchor=\"middle\" x=\"132.103\" y=\"-8.14062\" font-family=\"Menlo\" font-size=\"10.00\">a</text>\n",
       "</g>\n",
       "<!-- 140288582453088forward&#45;&gt;140288582453136forward -->\n",
       "<g id=\"edge1\" class=\"edge\"><title>140288582453088forward&#45;&gt;140288582453136forward</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M117.746,-59.6509C113.372,-68.3903 108.498,-78.1298 103.879,-87.3617\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"100.717,-85.8594 99.3716,-96.3685 106.977,-88.992 100.717,-85.8594\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f977b1f8850>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 简单的计算图\n",
    "a = Scalar(1.0, label='a')\n",
    "b = Scalar(2.0, label='b')\n",
    "c = a + b\n",
    "draw_graph(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\n",
       " -->\n",
       "<!-- Title: %3 Pages: 1 -->\n",
       "<svg width=\"272pt\" height=\"260pt\"\n",
       " viewBox=\"0.00 0.00 272.21 259.77\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 255.766)\">\n",
       "<title>%3</title>\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-255.766 268.205,-255.766 268.205,4 -4,4\"/>\n",
       "<!-- 140288582453856forward -->\n",
       "<g id=\"node1\" class=\"node\"><title>140288582453856forward</title>\n",
       "<polygon fill=\"lightgreen\" stroke=\"black\" stroke-width=\"2\" points=\"0,-0.5 0,-59.4219 76.2051,-59.4219 76.2051,-0.5 0,-0.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-47.4219\" font-family=\"Menlo\" font-size=\"10.00\">grad=None</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"0,-39.7812 76.2051,-39.7812 \"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-27.7812\" font-family=\"Menlo\" font-size=\"10.00\">value=2.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"0,-20.1406 76.2051,-20.1406 \"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-8.14062\" font-family=\"Menlo\" font-size=\"10.00\">b</text>\n",
       "</g>\n",
       "<!-- 140288582452944forward -->\n",
       "<g id=\"node4\" class=\"node\"><title>140288582452944forward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M59,-96.4219C59,-96.4219 111.205,-96.4219 111.205,-96.4219 117.205,-96.4219 123.205,-102.422 123.205,-108.422 123.205,-108.422 123.205,-143.344 123.205,-143.344 123.205,-149.344 117.205,-155.344 111.205,-155.344 111.205,-155.344 59,-155.344 59,-155.344 53,-155.344 47,-149.344 47,-143.344 47,-143.344 47,-108.422 47,-108.422 47,-102.422 53,-96.4219 59,-96.4219\"/>\n",
       "<text text-anchor=\"middle\" x=\"85.1025\" y=\"-143.344\" font-family=\"Menlo\" font-size=\"10.00\">grad=None</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"47,-135.703 123.205,-135.703 \"/>\n",
       "<text text-anchor=\"middle\" x=\"85.1025\" y=\"-123.703\" font-family=\"Menlo\" font-size=\"10.00\">value=3.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"47,-116.062 123.205,-116.062 \"/>\n",
       "<text text-anchor=\"middle\" x=\"85.1025\" y=\"-104.062\" font-family=\"Menlo\" font-size=\"10.00\">+</text>\n",
       "</g>\n",
       "<!-- 140288582453856forward&#45;&gt;140288582452944forward -->\n",
       "<g id=\"edge4\" class=\"edge\"><title>140288582453856forward&#45;&gt;140288582452944forward</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M52.4595,-59.6509C56.8328,-68.3903 61.7066,-78.1298 66.3264,-87.3617\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"63.2284,-88.992 70.8335,-96.3685 69.4883,-85.8594 63.2284,-88.992\"/>\n",
       "</g>\n",
       "<!-- 140288582454384forward -->\n",
       "<g id=\"node2\" class=\"node\"><title>140288582454384forward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M102.99,-192.344C102.99,-192.344 161.215,-192.344 161.215,-192.344 167.215,-192.344 173.215,-198.344 173.215,-204.344 173.215,-204.344 173.215,-239.266 173.215,-239.266 173.215,-245.266 167.215,-251.266 161.215,-251.266 161.215,-251.266 102.99,-251.266 102.99,-251.266 96.9897,-251.266 90.9897,-245.266 90.9897,-239.266 90.9897,-239.266 90.9897,-204.344 90.9897,-204.344 90.9897,-198.344 96.9897,-192.344 102.99,-192.344\"/>\n",
       "<text text-anchor=\"middle\" x=\"132.103\" y=\"-239.266\" font-family=\"Menlo\" font-size=\"10.00\">grad=None</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"90.9897,-231.625 173.215,-231.625 \"/>\n",
       "<text text-anchor=\"middle\" x=\"132.103\" y=\"-219.625\" font-family=\"Menlo\" font-size=\"10.00\">value=12.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"90.9897,-211.984 173.215,-211.984 \"/>\n",
       "<text text-anchor=\"middle\" x=\"132.103\" y=\"-199.984\" font-family=\"Menlo\" font-size=\"10.00\">*</text>\n",
       "</g>\n",
       "<!-- 140288582454432forward -->\n",
       "<g id=\"node3\" class=\"node\"><title>140288582454432forward</title>\n",
       "<polygon fill=\"lightgreen\" stroke=\"black\" stroke-width=\"2\" points=\"94,-0.5 94,-59.4219 170.205,-59.4219 170.205,-0.5 94,-0.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"132.103\" y=\"-47.4219\" font-family=\"Menlo\" font-size=\"10.00\">grad=None</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"94,-39.7812 170.205,-39.7812 \"/>\n",
       "<text text-anchor=\"middle\" x=\"132.103\" y=\"-27.7812\" font-family=\"Menlo\" font-size=\"10.00\">value=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"94,-20.1406 170.205,-20.1406 \"/>\n",
       "<text text-anchor=\"middle\" x=\"132.103\" y=\"-8.14062\" font-family=\"Menlo\" font-size=\"10.00\">a</text>\n",
       "</g>\n",
       "<!-- 140288582454432forward&#45;&gt;140288582452944forward -->\n",
       "<g id=\"edge2\" class=\"edge\"><title>140288582454432forward&#45;&gt;140288582452944forward</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M117.746,-59.6509C113.372,-68.3903 108.498,-78.1298 103.879,-87.3617\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"100.717,-85.8594 99.3716,-96.3685 106.977,-88.992 100.717,-85.8594\"/>\n",
       "</g>\n",
       "<!-- 140288582453136forward -->\n",
       "<g id=\"node6\" class=\"node\"><title>140288582453136forward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M153,-96.4219C153,-96.4219 205.205,-96.4219 205.205,-96.4219 211.205,-96.4219 217.205,-102.422 217.205,-108.422 217.205,-108.422 217.205,-143.344 217.205,-143.344 217.205,-149.344 211.205,-155.344 205.205,-155.344 205.205,-155.344 153,-155.344 153,-155.344 147,-155.344 141,-149.344 141,-143.344 141,-143.344 141,-108.422 141,-108.422 141,-102.422 147,-96.4219 153,-96.4219\"/>\n",
       "<text text-anchor=\"middle\" x=\"179.103\" y=\"-143.344\" font-family=\"Menlo\" font-size=\"10.00\">grad=None</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"141,-135.703 217.205,-135.703 \"/>\n",
       "<text text-anchor=\"middle\" x=\"179.103\" y=\"-123.703\" font-family=\"Menlo\" font-size=\"10.00\">value=4.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"141,-116.062 217.205,-116.062 \"/>\n",
       "<text text-anchor=\"middle\" x=\"179.103\" y=\"-104.062\" font-family=\"Menlo\" font-size=\"10.00\">*</text>\n",
       "</g>\n",
       "<!-- 140288582454432forward&#45;&gt;140288582453136forward -->\n",
       "<g id=\"edge6\" class=\"edge\"><title>140288582454432forward&#45;&gt;140288582453136forward</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M146.459,-59.6509C150.833,-68.3903 155.707,-78.1298 160.326,-87.3617\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"157.228,-88.992 164.834,-96.3685 163.488,-85.8594 157.228,-88.992\"/>\n",
       "</g>\n",
       "<!-- 140288582452944forward&#45;&gt;140288582454384forward -->\n",
       "<g id=\"edge1\" class=\"edge\"><title>140288582452944forward&#45;&gt;140288582454384forward</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M99.4595,-155.573C103.833,-164.312 108.707,-174.052 113.326,-183.284\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"110.228,-184.914 117.834,-192.29 116.488,-181.781 110.228,-184.914\"/>\n",
       "</g>\n",
       "<!-- 140288582455104forward -->\n",
       "<g id=\"node5\" class=\"node\"><title>140288582455104forward</title>\n",
       "<polygon fill=\"lightgreen\" stroke=\"black\" stroke-width=\"2\" points=\"188,-0.5 188,-59.4219 264.205,-59.4219 264.205,-0.5 188,-0.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"226.103\" y=\"-47.4219\" font-family=\"Menlo\" font-size=\"10.00\">grad=None</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"188,-39.7812 264.205,-39.7812 \"/>\n",
       "<text text-anchor=\"middle\" x=\"226.103\" y=\"-27.7812\" font-family=\"Menlo\" font-size=\"10.00\">value=4.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"188,-20.1406 264.205,-20.1406 \"/>\n",
       "<text text-anchor=\"middle\" x=\"226.103\" y=\"-8.14062\" font-family=\"Menlo\" font-size=\"10.00\">c</text>\n",
       "</g>\n",
       "<!-- 140288582455104forward&#45;&gt;140288582453136forward -->\n",
       "<g id=\"edge5\" class=\"edge\"><title>140288582455104forward&#45;&gt;140288582453136forward</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M211.746,-59.6509C207.372,-68.3903 202.498,-78.1298 197.879,-87.3617\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"194.717,-85.8594 193.372,-96.3685 200.977,-88.992 194.717,-85.8594\"/>\n",
       "</g>\n",
       "<!-- 140288582453136forward&#45;&gt;140288582454384forward -->\n",
       "<g id=\"edge3\" class=\"edge\"><title>140288582453136forward&#45;&gt;140288582454384forward</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M164.746,-155.573C160.372,-164.312 155.498,-174.052 150.879,-183.284\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"147.717,-181.781 146.372,-192.29 153.977,-184.914 147.717,-181.781\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f977b1f8eb0>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 稍微复杂的计算图\n",
    "a = Scalar(1.0, label='a')\n",
    "b = Scalar(2.0, label='b')\n",
    "c = Scalar(4.0, label='c')\n",
    "d = a + b\n",
    "e = a * c\n",
    "f = d * e\n",
    "backward_process = f.backward(draw_graph)\n",
    "draw_graph(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\n",
       " -->\n",
       "<!-- Title: %3 Pages: 1 -->\n",
       "<svg width=\"272pt\" height=\"292pt\"\n",
       " viewBox=\"0.00 0.00 272.21 292.36\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 288.359)\">\n",
       "<title>%3</title>\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-288.359 268.205,-288.359 268.205,4 -4,4\"/>\n",
       "<!-- 140288582453856backward -->\n",
       "<g id=\"node1\" class=\"node\"><title>140288582453856backward</title>\n",
       "<polygon fill=\"lightgreen\" stroke=\"black\" stroke-width=\"2\" points=\"0,-0.5 0,-59.4219 76.2051,-59.4219 76.2051,-0.5 0,-0.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-47.4219\" font-family=\"Menlo\" font-size=\"10.00\">grad=4.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"0,-39.7812 76.2051,-39.7812 \"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-27.7812\" font-family=\"Menlo\" font-size=\"10.00\">value=2.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"0,-20.1406 76.2051,-20.1406 \"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-8.14062\" font-family=\"Menlo\" font-size=\"10.00\">b</text>\n",
       "</g>\n",
       "<!-- 140288582454384backward -->\n",
       "<g id=\"node2\" class=\"node\"><title>140288582454384backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M104.99,-224.938C104.99,-224.938 163.215,-224.938 163.215,-224.938 169.215,-224.938 175.215,-230.938 175.215,-236.938 175.215,-236.938 175.215,-271.859 175.215,-271.859 175.215,-277.859 169.215,-283.859 163.215,-283.859 163.215,-283.859 104.99,-283.859 104.99,-283.859 98.9897,-283.859 92.9897,-277.859 92.9897,-271.859 92.9897,-271.859 92.9897,-236.938 92.9897,-236.938 92.9897,-230.938 98.9897,-224.938 104.99,-224.938\"/>\n",
       "<text text-anchor=\"middle\" x=\"134.103\" y=\"-271.859\" font-family=\"Menlo\" font-size=\"10.00\">grad=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"92.9897,-264.219 175.215,-264.219 \"/>\n",
       "<text text-anchor=\"middle\" x=\"134.103\" y=\"-252.219\" font-family=\"Menlo\" font-size=\"10.00\">value=12.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"92.9897,-244.578 175.215,-244.578 \"/>\n",
       "<text text-anchor=\"middle\" x=\"134.103\" y=\"-232.578\" font-family=\"Menlo\" font-size=\"10.00\">*</text>\n",
       "</g>\n",
       "<!-- 140288582452944backward -->\n",
       "<g id=\"node4\" class=\"node\"><title>140288582452944backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M57,-112.719C57,-112.719 109.205,-112.719 109.205,-112.719 115.205,-112.719 121.205,-118.719 121.205,-124.719 121.205,-124.719 121.205,-159.641 121.205,-159.641 121.205,-165.641 115.205,-171.641 109.205,-171.641 109.205,-171.641 57,-171.641 57,-171.641 51,-171.641 45,-165.641 45,-159.641 45,-159.641 45,-124.719 45,-124.719 45,-118.719 51,-112.719 57,-112.719\"/>\n",
       "<text text-anchor=\"middle\" x=\"83.1025\" y=\"-159.641\" font-family=\"Menlo\" font-size=\"10.00\">grad=4.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"45,-152 121.205,-152 \"/>\n",
       "<text text-anchor=\"middle\" x=\"83.1025\" y=\"-140\" font-family=\"Menlo\" font-size=\"10.00\">value=3.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"45,-132.359 121.205,-132.359 \"/>\n",
       "<text text-anchor=\"middle\" x=\"83.1025\" y=\"-120.359\" font-family=\"Menlo\" font-size=\"10.00\">+</text>\n",
       "</g>\n",
       "<!-- 140288582454384backward&#45;&gt;140288582452944backward -->\n",
       "<g id=\"edge1\" class=\"edge\"><title>140288582454384backward&#45;&gt;140288582452944backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M120.836,-224.727C114.638,-211.333 107.177,-195.207 100.531,-180.845\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"103.679,-179.313 96.3026,-171.707 97.3256,-182.252 103.679,-179.313\"/>\n",
       "<text text-anchor=\"middle\" x=\"128.96\" y=\"-195.238\" font-family=\"Menlo\" font-size=\"14.00\">4.00</text>\n",
       "</g>\n",
       "<!-- 140288582453136backward -->\n",
       "<g id=\"node6\" class=\"node\"><title>140288582453136backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M151,-112.719C151,-112.719 203.205,-112.719 203.205,-112.719 209.205,-112.719 215.205,-118.719 215.205,-124.719 215.205,-124.719 215.205,-159.641 215.205,-159.641 215.205,-165.641 209.205,-171.641 203.205,-171.641 203.205,-171.641 151,-171.641 151,-171.641 145,-171.641 139,-165.641 139,-159.641 139,-159.641 139,-124.719 139,-124.719 139,-118.719 145,-112.719 151,-112.719\"/>\n",
       "<text text-anchor=\"middle\" x=\"177.103\" y=\"-159.641\" font-family=\"Menlo\" font-size=\"10.00\">grad=3.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"139,-152 215.205,-152 \"/>\n",
       "<text text-anchor=\"middle\" x=\"177.103\" y=\"-140\" font-family=\"Menlo\" font-size=\"10.00\">value=4.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"139,-132.359 215.205,-132.359 \"/>\n",
       "<text text-anchor=\"middle\" x=\"177.103\" y=\"-120.359\" font-family=\"Menlo\" font-size=\"10.00\">*</text>\n",
       "</g>\n",
       "<!-- 140288582454384backward&#45;&gt;140288582453136backward -->\n",
       "<g id=\"edge3\" class=\"edge\"><title>140288582454384backward&#45;&gt;140288582453136backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M145.288,-224.727C150.464,-211.46 156.685,-195.514 162.248,-181.255\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"165.599,-182.295 165.973,-171.707 159.078,-179.751 165.599,-182.295\"/>\n",
       "<text text-anchor=\"middle\" x=\"175.96\" y=\"-195.238\" font-family=\"Menlo\" font-size=\"14.00\">3.00</text>\n",
       "</g>\n",
       "<!-- 140288582454432backward -->\n",
       "<g id=\"node3\" class=\"node\"><title>140288582454432backward</title>\n",
       "<polygon fill=\"lightgreen\" stroke=\"black\" stroke-width=\"2\" points=\"94,-0.5 94,-59.4219 170.205,-59.4219 170.205,-0.5 94,-0.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"132.103\" y=\"-47.4219\" font-family=\"Menlo\" font-size=\"10.00\">grad=16.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"94,-39.7812 170.205,-39.7812 \"/>\n",
       "<text text-anchor=\"middle\" x=\"132.103\" y=\"-27.7812\" font-family=\"Menlo\" font-size=\"10.00\">value=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"94,-20.1406 170.205,-20.1406 \"/>\n",
       "<text text-anchor=\"middle\" x=\"132.103\" y=\"-8.14062\" font-family=\"Menlo\" font-size=\"10.00\">a</text>\n",
       "</g>\n",
       "<!-- 140288582452944backward&#45;&gt;140288582453856backward -->\n",
       "<g id=\"edge4\" class=\"edge\"><title>140288582452944backward&#45;&gt;140288582453856backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M53.3927,-112.593C49.0008,-106.922 45.0605,-100.703 42.3877,-94.2188 39.2266,-86.5495 37.5553,-77.9139 36.7706,-69.5454\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"40.2648,-69.3424 36.2125,-59.5531 33.2757,-69.7328 40.2648,-69.3424\"/>\n",
       "<text text-anchor=\"middle\" x=\"59.96\" y=\"-83.0187\" font-family=\"Menlo\" font-size=\"14.00\">4.00</text>\n",
       "</g>\n",
       "<!-- 140288582452944backward&#45;&gt;140288582454432backward -->\n",
       "<g id=\"edge2\" class=\"edge\"><title>140288582452944backward&#45;&gt;140288582454432backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M95.8488,-112.509C101.747,-99.2417 108.836,-83.2956 115.175,-69.0367\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"118.556,-70.0479 119.42,-59.4883 112.16,-67.2042 118.556,-70.0479\"/>\n",
       "<text text-anchor=\"middle\" x=\"127.96\" y=\"-83.0187\" font-family=\"Menlo\" font-size=\"14.00\">4.00</text>\n",
       "</g>\n",
       "<!-- 140288582455104backward -->\n",
       "<g id=\"node5\" class=\"node\"><title>140288582455104backward</title>\n",
       "<polygon fill=\"lightgreen\" stroke=\"black\" stroke-width=\"2\" points=\"188,-0.5 188,-59.4219 264.205,-59.4219 264.205,-0.5 188,-0.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"226.103\" y=\"-47.4219\" font-family=\"Menlo\" font-size=\"10.00\">grad=3.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"188,-39.7812 264.205,-39.7812 \"/>\n",
       "<text text-anchor=\"middle\" x=\"226.103\" y=\"-27.7812\" font-family=\"Menlo\" font-size=\"10.00\">value=4.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"188,-20.1406 264.205,-20.1406 \"/>\n",
       "<text text-anchor=\"middle\" x=\"226.103\" y=\"-8.14062\" font-family=\"Menlo\" font-size=\"10.00\">c</text>\n",
       "</g>\n",
       "<!-- 140288582453136backward&#45;&gt;140288582454432backward -->\n",
       "<g id=\"edge6\" class=\"edge\"><title>140288582453136backward&#45;&gt;140288582454432backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M165.397,-112.509C159.98,-99.2417 153.47,-83.2956 147.648,-69.0367\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"150.77,-67.4235 143.75,-59.4883 144.289,-70.0694 150.77,-67.4235\"/>\n",
       "<text text-anchor=\"middle\" x=\"179.174\" y=\"-83.0187\" font-family=\"Menlo\" font-size=\"14.00\">12.00</text>\n",
       "</g>\n",
       "<!-- 140288582453136backward&#45;&gt;140288582455104backward -->\n",
       "<g id=\"edge5\" class=\"edge\"><title>140288582453136backward&#45;&gt;140288582455104backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M194.68,-112.568C198.018,-106.61 201.33,-100.293 204.103,-94.2188 207.747,-86.2361 211.154,-77.4624 214.148,-69.0579\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"217.516,-70.0256 217.461,-59.4309 210.897,-67.7473 217.516,-70.0256\"/>\n",
       "<text text-anchor=\"middle\" x=\"228.96\" y=\"-83.0187\" font-family=\"Menlo\" font-size=\"14.00\">3.00</text>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f977b1f8100>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "draw_graph(f, 'backward')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将反向传播的过程展示出来（可能会有弹框）\n",
    "for index, pic in enumerate(backward_process):\n",
    "    pic.view(str(index))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
